home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 11587 / 11587.xpi / chrome / aviary.jar / content / pearlutil.js < prev    next >
Encoding:
Text File  |  2009-08-11  |  21.9 KB  |  752 lines

  1. /* Copyright (c) 2005-2009 Pearl Crescent, LLC.  All Rights Reserved. */
  2. /* vim: set sw=2 sts=2 ts=8 et syntax=javascript: */
  3.  
  4. /*
  5.  * This file is part of the Pearl Crescent Utility Functions Library.
  6.  */
  7.  
  8. if (!com) var com = {};
  9. if (!com.aviary) com.aviary = {};
  10. if (!com.aviary.talon) com.aviary.talon = {};
  11. if (!com.aviary.talon.pearlutil) com.aviary.talon.pearlutil = {
  12.   kPropBundleURI : "chrome://aviary/locale/extension.properties",
  13.   kStringBundleCID : "@mozilla.org/intl/stringbundle;1",
  14.   kStringBundleInterface : Components.interfaces.nsIStringBundleService,
  15.   kMaxScrollbarSize : 40, // We assume no scrollbar is wider than this.
  16.   kLogPrefix: "Aviary",
  17.  
  18.   // Settings:
  19.   mDebugLevel: 0, // to set this, call SetDebugLevel()
  20.  
  21.   // Cached values:
  22.   mStringBundle: null,
  23.   mAppVersion: 0,
  24.   mConsoleSvc: null,
  25.   mHorzScrollBarHeight: 0,
  26.   _mPrefService: null,
  27.  
  28.   get mPrefService()
  29.   {
  30.     if (!this._mPrefService)
  31.     {
  32.       this._mPrefService = Components
  33.                               .classes["@mozilla.org/preferences-service;1"]
  34.                               .getService(Components.interfaces.nsIPrefBranch);
  35.     }
  36.  
  37.     return this._mPrefService;
  38.   },
  39.  
  40.   // Returns the major version only, e.g, 2.
  41.   GetAppVersion: function()
  42.   {
  43.     if (0 == this.mAppVersion)
  44.     try
  45.     {
  46.       var v = Components.classes["@mozilla.org/xre/app-info;1"]
  47.                      .getService(Components.interfaces.nsIXULAppInfo).version;
  48.       this.mAppVersion = parseInt(v);
  49.     } catch (e) {}
  50.  
  51.     return this.mAppVersion;
  52.   },
  53.  
  54.   // Pass null for aWindow to center on screen.
  55.   Alert: function(aWindow, aMsg)
  56.   {
  57.     try
  58.     {
  59.       var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  60.                          .getService(Components.interfaces.nsIPromptService);
  61.       ps.alert(aWindow, this.GetLocalizedString("ALERT_TITLE"), aMsg);
  62.     }
  63.     catch (e)
  64.     {
  65.       alert(aMsg);
  66.     }
  67.   },
  68.  
  69.   OpenURL: function(aURL, aEvent, aInNewTab)
  70.   {
  71.     if (aURL)
  72.     {
  73.       var tabBrowser = top.getBrowser();
  74.       if (aInNewTab)
  75.       {
  76.         var newtab = window.openNewTabWith(aURL, null, null, aEvent, false);
  77.         if (newtab)
  78.           tabBrowser.selectedTab = newtab;
  79.       }
  80.       else
  81.         tabBrowser.loadURI(aURL);
  82.     }
  83.   },
  84.  
  85.   // Returns true if a new window was opened.
  86.   OpenXULWindow: function(aWindowType, aChromeURL, aFeatures, aArg)
  87.   {
  88.     if (!aChromeURL)
  89.       return false;
  90.  
  91.     var winMed = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  92.                            .getService(Components.interfaces.nsIWindowMediator);
  93.     if (winMed && aWindowType)
  94.     {
  95.       var win = winMed.getMostRecentWindow(aWindowType);
  96.       if (win)
  97.       {
  98.         win.focus();
  99.         return false;
  100.       }
  101.     }
  102.  
  103.     window.openDialog(aChromeURL, "_blank", aFeatures, aArg);
  104.     return true;
  105.   },
  106.  
  107.   // Returns true if a new window was opened.
  108.   OpenPrefsWindow: function(aPrefWindowType, aChromeURL)
  109.   {
  110.     var features = "dialog=no,chrome,titlebar,toolbar";
  111.     var doInstantApply = this.GetBoolPref("browser.preferences.instantApply",
  112.                                           false);
  113.     if (!doInstantApply)
  114.       features += ",modal";
  115.  
  116.     return this.OpenXULWindow(aPrefWindowType, aChromeURL, features, null);
  117.   },
  118.  
  119.   /*
  120.    * If aBeforeID is present, add the toolbar item aItemID before it.
  121.    * If aBeforeID is not present, add the item to the end of the toolbar
  122.    *   identified by aToolbarID.
  123.    * Returns: true if item added to a toolbar (or if it was already present).
  124.    */
  125.   ShowToolbarItem: function(aDoc, aItemID, aBeforeID, aToolbarID)
  126.   {
  127.     var itemElem = aDoc.getElementById(aItemID);
  128.     if (itemElem)
  129.       return true; // Already present.
  130.  
  131.     var tbElem;
  132.     var beforeElem = aBeforeID ? aDoc.getElementById(aBeforeID) : null;
  133.     if (beforeElem)
  134.       tbElem = beforeElem.parentNode;
  135.     if (!tbElem)
  136.       tbElem = aDoc.getElementById(aToolbarID);
  137.     if (tbElem)
  138.     {
  139.       var newSet;
  140.       if (!tbElem.currentSet)
  141.         newSet = aItemID;
  142.       else
  143.       {
  144.         var start = tbElem.currentSet.indexOf(aBeforeID);
  145.         if (start == 0)
  146.           newSet = aItemID + "," + tbElem.currentSet;
  147.         else if (start > 0)
  148.         {
  149.           newSet = tbElem.currentSet.substring(0, start) + aItemID
  150.                    + "," + tbElem.currentSet.substring(start);
  151.         }
  152.         else
  153.           newSet = tbElem.currentSet + "," + aItemID;
  154.       }
  155.  
  156.       // Remove search.xml#searchbar observer before setting currentSet.
  157.       // See Mozilla bug #230086 (Destructor of XBL element isn't called...)
  158.       try
  159.       {
  160.         var elem = document.getElementById("searchbar");
  161.         if (elem && elem.parentNode && ((elem.parentNode == tbElem) ||
  162.                                         (elem.parentNode.parentNode == tbElem)))
  163.         {
  164.           var obsSvc = Components.classes["@mozilla.org/observer-service;1"]
  165.                          .getService(Components.interfaces.nsIObserverService);
  166.           obsSvc.removeObserver(elem, "browser-search-engine-modified");
  167.         }
  168.       }
  169.       catch (e) {}
  170.  
  171.       tbElem.setAttribute("currentset", newSet);
  172.       tbElem.currentSet = newSet;
  173.       aDoc.persist(tbElem.getAttribute("id"), "currentset");
  174.       return true;
  175.     }
  176.  
  177.     return false; // Not added.
  178.   }, // ShowToolbarItem()
  179.  
  180.   GetLocalizedString: function(aStringName)
  181.   {
  182.     try
  183.     {
  184.       if (!this.mStringBundle)
  185.       {
  186.         this.mStringBundle = Components.classes[this.kStringBundleCID]
  187.                                 .getService(this.kStringBundleInterface)
  188.                                 .createBundle(this.kPropBundleURI);
  189.       }
  190.  
  191.       return this.mStringBundle.GetStringFromName(aStringName);
  192.     } catch(e) {}
  193.  
  194.     return aStringName;
  195.   },
  196.  
  197.   GetFormattedLocalizedString: function(aStringName, aArray, aLen)
  198.   {
  199.     try
  200.     {
  201.       if (!this.mStringBundle)
  202.       {
  203.         this.mStringBundle = Components.classes[this.kStringBundleCID]
  204.                             .getService(this.kStringBundleInterface)
  205.                             .createBundle(this.kPropBundleURI);
  206.       }
  207.  
  208.       return this.mStringBundle.formatStringFromName(aStringName, aArray, aLen);
  209.     } catch(e) {}
  210.  
  211.     return aStringName;
  212.   },
  213.  
  214.   GetFirstChildText: function(aParentElem, aTagName)
  215.   {
  216.     var e = this.PearlGetFirstElementByTagName(aParentElem, aTagName);
  217.     return this.GetNodeText(e);
  218.   },
  219.  
  220.   GetNodeText: function(aNode)
  221.   {
  222.     if (aNode)
  223.     {
  224.       var node = aNode.firstChild;
  225.       if (node &&
  226.          (node.nodeType == Components.interfaces.nsIDOMNode.TEXT_NODE ||
  227.           node.nodeType == Components.interfaces.nsIDOMNode.CDATA_SECTION_NODE))
  228.       {
  229.         return node.nodeValue;
  230.       }
  231.     }
  232.  
  233.     return "";
  234.   },
  235.  
  236.   PearlGetFirstElementByTagName: function(aParentElem, aTagName)
  237.   {
  238.     var firstElem;
  239.  
  240.     if (aParentElem && aTagName)
  241.     {
  242.       var elemList = aParentElem.getElementsByTagName(aTagName);
  243.       if (elemList && elemList.length > 0)
  244.         firstElem = elemList.item(0);
  245.     }
  246.  
  247.     return firstElem;
  248.   },
  249.  
  250.   // GetElementsByAttr() is used by pearlutil-grab.js
  251.   // Returns an array of elements or null if no elements were found.
  252.   // aValue may be null, in which case the value is not checked.
  253.   GetElementsByAttr: function(aContainerElem, aAttr, aValue)
  254.   {
  255.  
  256.     var elemArray = new Array();
  257.     this._GetElementsByAttr(aContainerElem, aAttr, aValue, elemArray);
  258.     return (elemArray.length > 0) ? elemArray : null;
  259.   },
  260.  
  261.   _GetElementsByAttr: function(aContainerElem, aAttr, aValue, aElemArray)
  262.   {
  263.     if (!aContainerElem || !aAttr || !aElemArray)
  264.       return;
  265.   
  266.     for (var childElem = aContainerElem.firstChild; childElem;
  267.          childElem = childElem.nextSibling)
  268.     {
  269.       if (childElem.hasAttribute(aAttr)
  270.           && (!aValue || aValue == childElem.getAttribute(aAttr)))
  271.       {
  272.         aElemArray.push(childElem);
  273.       }
  274.  
  275.       if (childElem.firstChild)
  276.         this._GetElementsByAttr(childElem, aAttr, aValue, aElemArray);
  277.     }
  278.   },
  279.  
  280.   SetElementText: function(aElem, aText)
  281.   {
  282.     if (!aElem)
  283.       return;
  284.  
  285.     var node = aElem.firstChild;
  286.     if (node)
  287.       aElem.removeChild(node);
  288.  
  289.     if (aText && aText.length > 0)
  290.     {
  291.       var textNode = document.createTextNode(aText);
  292.       if (textNode)
  293.         aElem.appendChild(textNode);
  294.     }
  295.   },
  296.  
  297.   SetElementValue: function(aElem, aValue)
  298.   {
  299.     if (!aElem)
  300.       return;
  301.     if (!aValue || aValue.length == 0)
  302.       aValue = "";
  303.     aElem.value = aValue;
  304.   },
  305.  
  306.   SetElementAttribute: function(aElemID, aAttr, aValue)
  307.   {
  308.     var e = document.getElementById(aElemID);
  309.     if (e)
  310.     {
  311.       if (aValue)
  312.         e.setAttribute(aAttr, aValue);
  313.       else
  314.         e.removeAttribute(aAttr);
  315.     }
  316.   },
  317.  
  318.   GetElementAttribute: function(aElemID, aAttr, aDefaultValue)
  319.   {
  320.     var e = document.getElementById(aElemID);
  321.     var retVal = "";
  322.     if (e && aAttr)
  323.       retVal = e.getAttribute(aAttr);
  324.     if (!retVal)
  325.       retVal = aDefaultValue;
  326.  
  327.     return retVal;
  328.   },
  329.  
  330.   EnableElement: function(aID, aIsEnabled)
  331.   {
  332.     this.SetElementAttribute(aID, "disabled", !aIsEnabled);
  333.   },
  334.  
  335.   GetTopBrowserWindow: function()
  336.   {
  337.     var winMed = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  338.                            .getService(Components.interfaces.nsIWindowMediator);
  339.     var winList = winMed.getZOrderDOMWindowEnumerator("navigator:browser",
  340.                                                       true);
  341.     if (!winList.hasMoreElements())
  342.       return top.getBrowser().contentWindow; // fallback
  343.  
  344.     return winList.getNext().getBrowser().contentWindow;
  345.   },
  346.  
  347.   HasClipboardData: function(aMimeType)
  348.   {
  349.     if (!aMimeType)
  350.       return false;
  351.  
  352.     try
  353.     {
  354.       const knsIClipboard = Components.interfaces.nsIClipboard;
  355.  
  356.       var cbSvc = Components.classes["@mozilla.org/widget/clipboard;1"]
  357.                             .getService(knsIClipboard);
  358.       var typeArray = [ aMimeType ];
  359.       return cbSvc.hasDataMatchingFlavors(typeArray, typeArray.length,
  360.                                           knsIClipboard.kGlobalClipboard);
  361.  
  362.     } catch (e) {}
  363.  
  364.     return false;
  365.   },
  366.  
  367.   // aMimeType might be:  "image/png" or "image/jpg"
  368.   // This currently works for image types but not string types.
  369.   GetClipboardAsDataURI: function(aMimeType)
  370.   {
  371.     if (!aMimeType)
  372.       return null;
  373.  
  374.     try
  375.     {
  376.       const knsIClipboard = Components.interfaces.nsIClipboard;
  377.       const knsIInputStream = Components.interfaces.nsIInputStream;
  378.       const knsIBinaryInputStream = Components.interfaces.nsIBinaryInputStream;
  379.  
  380.       var cbSvc = Components.classes["@mozilla.org/widget/clipboard;1"]
  381.                             .getService(knsIClipboard);
  382.       var xfer = Components.classes["@mozilla.org/widget/transferable;1"]
  383.                          .createInstance(Components.interfaces.nsITransferable);
  384.       xfer.addDataFlavor(aMimeType);
  385.       cbSvc.getData(xfer, knsIClipboard.kGlobalClipboard);
  386.       var typeObj = {}, dataObj = {}, lenObj = {};
  387.       xfer.getAnyTransferData(typeObj, dataObj, lenObj);
  388.  
  389.       // TODO: handle string types (they do not have input streams).
  390.       var imgStream = dataObj.value.QueryInterface(knsIInputStream);
  391.       var binStream = Components.classes["@mozilla.org/binaryinputstream;1"]
  392.                                 .createInstance(knsIBinaryInputStream);
  393.       binStream.setInputStream(imgStream);
  394.  
  395.       var len = binStream.available();
  396.  
  397.       return "data:" + aMimeType + ";base64," + btoa(binStream.readBytes(len));
  398.     }
  399.     catch(e) {}
  400.  
  401.     return null;
  402.   },
  403.  
  404.   // Return height of window client area.
  405.   GetWindowHeight: function(aWindow, aEntirePage)
  406.   {
  407.     /*
  408.      * Use offsetHeight of document element unless it is too small (for example,
  409.      * some pages have an offsetHeight 20 even though scrollMaxY is large).
  410.      * Our fallback is to use window.innerHeight which will be too large if a
  411.      * horizontal scrollbar is present.
  412.      *
  413.      * Avoid docElement.clientHeight because in standards compliance mode it
  414.      * only includes the visible height.
  415.      */
  416.     var height = -1;
  417.     try
  418.     {
  419.       var hasHorzSB = (aWindow.scrollMaxX > 0);
  420.       var sbMaxHeight = (hasHorzSB) ? this.kMaxScrollbarSize : 0;
  421.       var altWinHeight = aWindow.innerHeight + aWindow.scrollMaxY - sbMaxHeight;
  422.  
  423.       var docElement = aWindow.document.documentElement;
  424.       if (docElement && docElement.offsetHeight
  425.           && docElement.offsetHeight > aWindow.scrollMaxY
  426.           && altWinHeight <= docElement.offsetHeight)
  427.       {
  428.         // Ideal Calculation of window's height.
  429.         height = docElement.offsetHeight;
  430.         if (!aEntirePage)
  431.           height -= aWindow.scrollMaxY;
  432.       }
  433.  
  434.       if (height < 0)
  435.       {
  436.         // Alternate calculation (includes height of horz scrollbar if unknown).
  437.         if (hasHorzSB && 0 == this.mHorzScrollBarHeight) try
  438.         {
  439.           // Add hidden element with height=100% and use to determine SB height.
  440.           var tmpElem = aWindow.document.createElement("div");
  441.           tmpElem.setAttribute("style", "visibility: hidden; z-index: -1;"
  442.                                + " position: fixed; top: 0px; left: 0px;"
  443.                                + " margin: 0px; padding: 0px; border: none;"
  444.                                + " width: 100%; height: 100%");
  445.           aWindow.document.body.appendChild(tmpElem);
  446.           var h = aWindow.innerHeight - tmpElem.offsetHeight;
  447.           if (h > 0 && h < this.kMaxScrollbarSize)
  448.             this.mHorzScrollBarHeight = h;
  449.           aWindow.document.body.removeChild(tmpElem);
  450.         } catch (e) {}
  451.  
  452.         height = aWindow.innerHeight; // Includes height of horz. SB if present.
  453.         if (hasHorzSB && this.mHorzScrollBarHeight > 0)
  454.           height -= this.mHorzScrollBarHeight;
  455.         if (aEntirePage)
  456.           height += aWindow.scrollMaxY;
  457.       }
  458.     } catch(e) {}
  459.  
  460.     if (height < 0)
  461.       height = 0;
  462.  
  463.     return height;
  464.   },
  465.  
  466.   // Return width of window client area.
  467.   GetWindowWidth: function(aWindow, aEntirePage)
  468.   {
  469.     var browserWidth = 0;
  470.     try
  471.     {
  472.       browserWidth = aWindow.innerWidth; // our fallback
  473.       var docElement = aWindow.document.documentElement;
  474.       if (docElement && docElement.clientWidth)
  475.         browserWidth = docElement.clientWidth; // usually more accurate
  476.  
  477.       if (aEntirePage)
  478.         browserWidth += aWindow.scrollMaxX;
  479.     } catch(e) {}
  480.  
  481.     // dump("GetWindowWidth(): returning width: " + browserWidth + "\n");
  482.     return browserWidth;
  483.   },
  484.  
  485.   // Compute offset from top-left corner of document (recursive function).
  486.   // Use this like so:  var topLeft = this.GetOffsetsForNode(node, null);
  487.   // The result (topLeft) will contain two properties: top, left.
  488.   GetOffsetsForNode: function(aNode, aTopLeftPt)
  489.   {
  490.     if (!aTopLeftPt)
  491.     {
  492.       aTopLeftPt = new Object();
  493.       aTopLeftPt.left = 0;
  494.       aTopLeftPt.top = 0;
  495.     }
  496.  
  497.     if (!aNode)
  498.       return aTopLeftPt;
  499.  
  500.     aTopLeftPt.left += aNode.offsetLeft;
  501.     aTopLeftPt.top += aNode.offsetTop;
  502.     return this.GetOffsetsForNode(aNode.offsetParent, aTopLeftPt);
  503.   },
  504.  
  505.   SanitizeFileName: function(aName)
  506.   {
  507.     var fileName = null;
  508.     if (aName && aName.length > 0)
  509.     {
  510.       fileName = aName.replace(/[\/\\:]/g, "-");
  511.       fileName = fileName.replace(/[\*\?\"\<\>\|]+/g, "");
  512.       fileName = fileName.replace(/^\s+|\s+$/g, "");
  513.       fileName = fileName.substring(0, 216); // Windows limitation
  514.     }
  515.  
  516.     if (!fileName || 0 == fileName.length)
  517.       fileName = this.GetLocalizedString("SUGGESTED_FILE_PREFIX");
  518.  
  519.     return fileName;
  520.   },
  521.  
  522.   // Find a file that does not exist within the directory named by aDirKey.
  523.   // Returns an nsILocalFile or null.
  524.   // The file name  will be: aFileBaseName + ' ' + <number> + aFileExt,
  525.   //   e.g., "My Picture 1.png"
  526.   // If all files named using 1-99 exist, a name without the space or a
  527.   //   number is used.
  528.   // aDirKey may be "Desk" or any of the constants defined in
  529.   //   xpcom/io/nsDirectoryServiceDefs.h
  530.   GetFileLocation: function(aDirKey, aFileBaseName, aFileExt)
  531.   {
  532.     var fileLoc = null;
  533.     if (aDirKey && aFileBaseName && aFileExt)
  534.     try {
  535.       var dirSvc =  Components.classes["@mozilla.org/file/directory_service;1"]
  536.                               .getService(Components.interfaces.nsIProperties);
  537.       var dirLoc = dirSvc.get(aDirKey, Components.interfaces.nsILocalFile);
  538.       var fileLoc = null;
  539.       for (var i = 1; (null == fileLoc) && i < 100; ++i)
  540.       {
  541.         var tmpLoc = dirLoc.clone();
  542.         tmpLoc.append(aFileBaseName + ' ' + i + aFileExt);
  543.         if (!tmpLoc.exists())
  544.           fileLoc = tmpLoc;
  545.       }
  546.  
  547.       if (!fileLoc)
  548.       {
  549.         fileLoc = dirLoc.clone();
  550.         fileLoc.append(aFileBaseName + aFileExt);
  551.       }
  552.     } catch (e) {}
  553.  
  554.     return fileLoc;
  555.   },
  556.  
  557.   // Display the "Save As" dialog.  aFile is an nsILocalFile which, if non-null,
  558.   //   specifies the starting directory and default file name.
  559.   FilePickerSaveAs: function(aPrompt, aFilterName, aFileExt, aFile)
  560.   {
  561.     var fileLoc = null;
  562.     try
  563.     {
  564.       const knsIFilePicker = Components.interfaces.nsIFilePicker;
  565.       var filePicker =  Components.classes["@mozilla.org/filepicker;1"]
  566.                                   .createInstance(knsIFilePicker);
  567.       filePicker.init(window, aPrompt, knsIFilePicker.modeSave);
  568.       filePicker.appendFilter(aFilterName, '*' + aFileExt);
  569.       filePicker.defaultExtension = aFileExt.substring(1);
  570.       if (aFile)
  571.       {
  572.         filePicker.displayDirectory = aFile.parent;
  573.         filePicker.defaultString = aFile.leafName;
  574.       }
  575.  
  576.       var rv = filePicker.show();
  577.       if (knsIFilePicker.returnCancel != rv)
  578.         fileLoc = filePicker.file;
  579.     } catch (e) {}
  580.  
  581.     return fileLoc;
  582.   },
  583.  
  584.   // Returns 0 if successful and an nserror code if not.
  585.   WriteStreamToFile: function(aSourceStream, aDestFile)
  586.   {
  587.     var resultCode = 0;
  588.  
  589.     try
  590.     {
  591.       var fileOutStream =
  592.           Components.classes["@mozilla.org/network/file-output-stream;1"]
  593.                     .createInstance(Components.interfaces.nsIFileOutputStream);
  594.       const kWriteOnly = 0x02; // see prio.h
  595.       const kCreateFile = 0x08;
  596.       const kTruncate = 0x20;
  597.       var flags = kWriteOnly | kCreateFile | kTruncate;
  598.       fileOutStream.init(aDestFile, flags, 0666, false);
  599.  
  600.       const kIOBlockSize = 65536;
  601.       var remaining = aSourceStream.available();
  602.       while (remaining > 0)
  603.       {
  604.         var count = (remaining > kIOBlockSize) ? kIOBlockSize : remaining;
  605.         var binData = aSourceStream.readBytes(count);
  606.         fileOutStream.write(binData, count);
  607.         remaining -= count;
  608.       }
  609.  
  610.       fileOutStream.close();
  611.     }
  612.     catch (e)
  613.     {
  614.       dump("WriteStreamToFile error: " + e + "\n"); // log to aid debugging
  615.       resultCode = e.result;
  616.     }
  617.  
  618.     return resultCode;
  619.   },
  620.  
  621.   // Preference related functions that do not throw.
  622.   PrefHasUserValue: function(aPrefName)
  623.   {
  624.     try
  625.     {
  626.       return this.mPrefService.prefHasUserValue(aPrefName);
  627.     } catch (e) {}
  628.  
  629.     return false;
  630.   },
  631.  
  632.   ClearUserPref: function(aPrefName)
  633.   {
  634.     try
  635.     {
  636.       this.mPrefService.clearUserPref(aPrefName);
  637.     } catch (e) {}
  638.   },
  639.  
  640.   GetBoolPref: function(aPrefName, aDefaultValue /* = false */)
  641.   {
  642.     try
  643.     {
  644.       return this.mPrefService.getBoolPref(aPrefName);
  645.     } catch (e) {}
  646.  
  647.     return (aDefaultValue != undefined) ? aDefaultValue : false;
  648.   },
  649.  
  650.   SetBoolPref: function(aPrefName, aValue)
  651.   {
  652.     try
  653.     {
  654.       this.mPrefService.setBoolPref(aPrefName, aValue);
  655.     } catch (e) {}
  656.   },
  657.  
  658.   GetIntPref: function(aPrefName, aDefaultValue /* = 0 */)
  659.   {
  660.     try
  661.     {
  662.       return this.mPrefService.getIntPref(aPrefName);
  663.     } catch (e) {}
  664.  
  665.     return (aDefaultValue != undefined) ? aDefaultValue : 0;
  666.   },
  667.  
  668.   SetIntPref: function(aPrefName, aValue)
  669.   {
  670.     try
  671.     {
  672.       this.mPrefService.setIntPref(aPrefName, aValue);
  673.     } catch (e) {}
  674.   },
  675.  
  676.   GetASCIIPref: function(aPrefName, aDefaultValue /* = null */)
  677.   {
  678.     try
  679.     {
  680.       return this.mPrefService.getCharPref(aPrefName);
  681.     } catch (e) {}
  682.  
  683.     return (aDefaultValue != undefined) ? aDefaultValue : null;
  684.   },
  685.  
  686.   SetASCIIPref: function(aPrefName, aValue)
  687.   {
  688.     try
  689.     {
  690.       this.mPrefService.setCharPref(aPrefName, aValue);
  691.     } catch (e) {}
  692.   },
  693.  
  694.   GetLocalizedStrPref: function(aPrefName, aDefaultValue /* null */)
  695.   {
  696.     try
  697.     {
  698.       return this.mPrefService.getComplexValue(aPrefName,
  699.                             Components.interfaces.nsIPrefLocalizedString).data;
  700.     } catch (e) {}
  701.  
  702.     return (aDefaultValue != undefined) ? aDefaultValue : null;
  703.   },
  704.  
  705.   SetLocalizedStrPref: function(aPrefName, aValue)
  706.   {
  707.     try
  708.     {
  709.       const kISupStr = Components.interfaces.nsISupportsString;
  710.       var ss = Components.classes["@mozilla.org/supports-string;1"]
  711.                          .createInstance(kISupStr);
  712.       ss.data = aValue;
  713.       this.mPrefService.setComplexValue(aPrefName, kISupStr, ss);
  714.     } catch (e) {}
  715.   },
  716.  
  717.   SetDebugLevel: function(aLevel)
  718.   {
  719.     this.mDebugLevel = aLevel;
  720.   },
  721.  
  722.   TraceLog: function(aLevel, aModuleName, aMsg)
  723.   {
  724.     const doIncludeTimestamp = false;
  725.  
  726.     if (aLevel <= this.mDebugLevel)
  727.     {
  728.       if (!this.mConsoleSvc) try
  729.       {
  730.           this.mConsoleSvc = Components.classes["@mozilla.org/consoleservice;1"]
  731.                          .getService(Components.interfaces.nsIConsoleService);
  732.       }
  733.       catch (e) {}
  734.  
  735.       if (this.mConsoleSvc)
  736.       {
  737.         if (!aModuleName)
  738.           aModuleName = "";
  739.  
  740.         var s = this.kLogPrefix + " ";
  741.         if (doIncludeTimestamp)
  742.           s += new Date().toLocaleString() + ' ';
  743.         s += aModuleName + ": " + aMsg;
  744.         this.mConsoleSvc.logStringMessage(s);
  745.         dump(s + "\n");
  746.       }
  747.     }
  748.   },
  749.  
  750.   endOfObject: true
  751. }
  752.